package com.dondedesigns.eh.persistence;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table (name="BLUSERS")
public class BlUser {

	@Id @GeneratedValue
	@Column (name="ID_BLUSERS")
	private Long id;
	
	@Column (name="USERNAME")
	private String username;
	
	@Column (name="ALIVE_STATUS")
	private AliveStatus aliveStatus;
	
	@Column (name="UG_STATUS")
	private UgStatus ugStatus;
	
	@Column (name="RANK")
	private Rank rank;
	
	@ManyToOne (cascade=CascadeType.ALL)
	@JoinColumn (name="USER")
	private User user; 
	
	@OneToMany (cascade=CascadeType.ALL, mappedBy="blUser", fetch=FetchType.EAGER)
	private Set<Bullet> bullets;
	
	@OneToMany (cascade=CascadeType.ALL, mappedBy="blUser")
	private Set<Taxes> taxes;

	@Column (name="BULLET_UPDATE")
	private Date lastBulletUpdate;
	
	public BlUser() {
		bullets = new HashSet<Bullet>();
		taxes = new HashSet<Taxes>();
		username = "";
	}

	public Long getId() {
		return id;
	}

	@SuppressWarnings("unused")
	private void setId(Long id) {
		this.id = id;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public AliveStatus getAliveStatus() {
		return aliveStatus;
	}

	public void setAliveStatus(AliveStatus aliveStatus) {
		this.aliveStatus = aliveStatus;
	}
	
	public void setAliveStatus(String aliveStatus) {
		this.aliveStatus = AliveStatus.values()[Integer.parseInt(aliveStatus)];
	}

	public UgStatus getUgStatus() {
		return ugStatus;
	}

	public void setUgStatus(UgStatus ugStatus) {
		this.ugStatus = ugStatus;
	}
	
	public void setUgStatus(String ugStatus) {
		this.ugStatus = UgStatus.values()[Integer.parseInt(ugStatus)];
	}

	public Rank getRank() {
		return rank;
	}

	public void setRank(Rank rank) {
		this.rank = rank;
	}
	
	public void setRank(String rank) {
		this.rank = Rank.values()[Integer.parseInt(rank)];
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}
	
	public Set<Bullet> getBullets() {
		return bullets;
	}

	public void setBullets(Set<Bullet> bullets) {
		this.bullets = bullets;
	}

	public void setTaxes(Set<Taxes> taxes) {
		this.taxes = taxes;
	}

	public Set<Taxes> getTaxes() {
		return taxes;
	}
	
	public void setLastBulletUpdate(Date updateDate) {
		this.lastBulletUpdate = (Date) updateDate.clone();
		
	}

	public Date getLastBulletUpdate() {
		return lastBulletUpdate;
	}
	
	public void addBullet (Bullet bullet) {
		this.getBullets().add(bullet);
		bullet.setBlUser(this);
	}
	
	public void addTax (Taxes tax) {
		this.getTaxes().add(tax);
		tax.setBlUser(this);
	}
	
	@Override
	public int hashCode() {
		return username.hashCode();
	}
	
	@Override
	public boolean equals(Object obj) {
		return (obj instanceof BlUser) && 
				((BlUser)obj).username.equals(username);
	}

	public void addBullets(Bullet bullet) {
		this.getBullets().add(bullet);
		bullet.setBlUser(this);
	}
}
